package cn.iocoder.yudao.module.dr.service.source;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.dr.controller.admin.report.vo.index.*;
import cn.iocoder.yudao.module.dr.controller.admin.source.vo.SourceCatalogWithSourceDataRespVO;
import cn.iocoder.yudao.module.dr.controller.admin.source.vo.SourcePageReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.source.vo.SourceSaveReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.sourcecatalog.vo.SourceCatalogPageReqVO;
import cn.iocoder.yudao.module.dr.dal.dataobject.source.SourceDO;
import cn.iocoder.yudao.module.dr.dal.mysql.source.SourceMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.dr.enums.ErrorCodeConstants.SOURCE_NOT_EXISTS;

/**
 * 资源信息 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class SourceServiceImpl implements SourceService {

    @Resource
    private SourceMapper sourceMapper;

    @Override
    public Long createSource(SourceSaveReqVO createReqVO) {
        // 插入
        SourceDO source = BeanUtils.toBean(createReqVO, SourceDO.class);
        source.setSourceStatus("0");
        sourceMapper.insert(source);
        // 返回
        return source.getId();
    }

    @Override
    public void updateSource(SourceSaveReqVO updateReqVO) {
        // 校验存在
        validateSourceExists(updateReqVO.getId());
        // 更新
        SourceDO updateObj = BeanUtils.toBean(updateReqVO, SourceDO.class);
        sourceMapper.updateById(updateObj);
    }

    @Override
    public void deleteSource(Long id) {
        // 校验存在
        validateSourceExists(id);
        // 删除
        sourceMapper.deleteById(id);
    }

    private void validateSourceExists(Long id) {
        if (sourceMapper.selectById(id) == null) {
            throw exception(SOURCE_NOT_EXISTS);
        }
    }

    @Override
    public SourceDO getSource(Long id) {
        return sourceMapper.selectById(id);
    }

    @Override
    public PageResult<SourceDO> getSourcePage(SourcePageReqVO pageReqVO) {
        return sourceMapper.selectPage(pageReqVO);
    }

    @Override
    public List<SourceDO> queryByIds(List<Long> list) {
        if(CollectionUtils.isEmpty(list)){
            return Collections.emptyList();
        }
        return sourceMapper.queryByIds(list);
    }

    @Override
    public PageResult<SourceDO> selectPendingList(SourcePageReqVO pageReqVO) {
        Page<SourceDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        IPage<SourceDO> selectList = sourceMapper.selectPendingList(page,pageReqVO);
        PageResult<SourceDO> result = new PageResult<>();
        result.setTotal(selectList.getTotal());
        result.setList(selectList.getRecords());
        return result;
    }

    @Override
    public PageResult<SourceDO> selectApply(SourcePageReqVO pageReqVO) {
        Page<SourceDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        IPage<SourceDO> selectList = sourceMapper.selectApply(page,pageReqVO);
        PageResult<SourceDO> result = new PageResult<>();
        result.setTotal(selectList.getTotal());
        result.setList(selectList.getRecords());
        return result;
    }
    @Override
    public PageResult<SourceCatalogWithSourceDataRespVO> searchSource(SourceCatalogPageReqVO pageReqVO) {
        Page<SourceCatalogWithSourceDataRespVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        IPage<SourceCatalogWithSourceDataRespVO> selectList = sourceMapper.searchSource(page,pageReqVO);
        PageResult<SourceCatalogWithSourceDataRespVO> result = new PageResult<>();
        result.setTotal(selectList.getTotal());
        result.setList(selectList.getRecords());
        return result;
    }

    @Override
    public PageResult<SourceCatalogWithSourceDataRespVO> selectUseApply(SourceCatalogPageReqVO pageReqVO) {
        Page<SourceCatalogWithSourceDataRespVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        // 预处理 catalogTags，将字符串转换为 List
        if (pageReqVO.getCatalogTags() != null && !pageReqVO.getCatalogTags().isEmpty()) {
            List<String> tagList = Arrays.asList(pageReqVO.getCatalogTags().split(","));
            pageReqVO.setCatalogTagList(tagList); // 需要添加这个字段到 VO
        }
        IPage<SourceCatalogWithSourceDataRespVO> selectList = sourceMapper.selectUseApply(page,pageReqVO);
        PageResult<SourceCatalogWithSourceDataRespVO> result = new PageResult<>();
        result.setTotal(selectList.getTotal());
        result.setList(selectList.getRecords());
        return result;
    }

    @Override
    public List<SourceCatalogWithSourceDataRespVO> queryListByIds(List<Long> list) {
        return sourceMapper.queryListByIds(list);
    }

    @Override
    public List<SourceTypeDataRespVO> countBySourceType() {
        return sourceMapper.countBySourceType();
    }

    @Override
    public List<FileTypeDataRespVO> countByFileType() {
        return sourceMapper.countByFileType();
    }

    @Override
    public List<DeptUseDataRespVO> countByDeptUse() {
        return sourceMapper.countByDeptUse();
    }

    @Override
    public List<VisitDataRespVO> countByVisit() {
        return sourceMapper.countByVisit();
    }

    @Override
    public List<SourceTypeDataRespVO> countUseBySourceType() {
        return sourceMapper.countUseBySourceType();
    }

    @Override
    public List<FileTypeDataRespVO> countUseByFileType() {
        return sourceMapper.countUseByFileType();
    }

    @Override
    public List<DeptUseDataRespVO> countUseByDept() {
        return sourceMapper.countUseByDept();
    }

    @Override
    public List<SourceTypeDataRespVO> countVisitBySourceType() {
        return sourceMapper.countVisitBySourceType();
    }

    @Override
    public List<FileTypeDataRespVO> countVisitByFileType() {
        return sourceMapper.countVisitByFileType();
    }

    @Override
    public List<VisitDataRespVO> countVisitByTime() {
        return sourceMapper.countVisitByTime();
    }

    @Override
    public List<VisitMothDataRespVO> countVisitByMoth() {
        return sourceMapper.countVisitByMoth();
    }



}